package com.ftp.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 用于在FTP服务器创建目录的类
 */
public class FtpDirectoryOperate {

    private FTPClient ftpClient;

    public FtpDirectoryOperate(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }

    /**
     * 创建多层目录 , 如果有ftp服务器已存在该文件，则不创建，如果无，则创建
     *
     * @param remote 远程目录
     * @return
     */
    public void createDirectory(String remote) throws IOException {

        // 要创建的目录
        String directory = remote + "/";
        // 如果ftp不存在该目录，则递归创建目录
        if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(directory)) {
            // 开始
            int start = 0;
            // 结束
            int end = directory.indexOf("/", start);

            //用来记录每一级目录
            String path = "";

            do {
                //子目录
                String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), StandardCharsets.ISO_8859_1);
                path = path + "/" + subDirectory;
                if (!existFile(path)) {//如果该目录下不存在文件
                    if (makeDirectory(subDirectory)) {//如果创建目录成功
                        //改变工作目录
                        changeWorkingDirectory(subDirectory);
                    } else {
                        System.out.println("创建目录[" + subDirectory + "]失败");
                        changeWorkingDirectory(subDirectory);
                    }
                } else {//如果存在文件
                    changeWorkingDirectory(subDirectory);
                }
                start = end + 1;
                end = directory.indexOf("/", start);
                // 检查所有目录是否创建完毕
            } while (end > start);

        }
    }

    /**
     * 改变目录路径
     *
     * @param directory
     * @return
     */
    private boolean changeWorkingDirectory(String directory) {
        boolean flag = true;
        try {
            flag = ftpClient.changeWorkingDirectory(directory);
            if (flag) {
                System.out.println("进入文件夹" + directory + " 成功！");

            } else {
                System.out.println("进入文件夹" + directory + " 失败！开始创建文件夹");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return flag;
    }

    //判断ftp服务器文件是否存在
    private boolean existFile(String path) throws IOException {
        boolean flag = false;
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);
        if (ftpFileArr.length > 0) {
            flag = true;
        }
        return flag;
    }

    //创建目录
    private boolean makeDirectory(String dir) {
        boolean flag = true;
        try {
            flag = ftpClient.makeDirectory(dir);
            if (flag) {
                System.out.println("创建文件夹" + dir + " 成功！");

            } else {
                System.out.println("创建文件夹" + dir + " 失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

}
